package com.ztrue.common.utilities;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by zhangwei2 on 2016/1/19.
 * 日期工具
 */
public final class DateUtil {
    /** 日期格式 **/
    public interface DATE_PATTERN {
        String HHMMSS = "HHmmss";
        String HH_MM_SS = "HH:mm:ss";
        String YYYYMMDD = "yyyyMMdd";
        String YYYY_MM_DD = "yyyy-MM-dd";
        String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
        String YYYYMMDDHHMMSSSSS = "yyyyMMddHHmmssSSS";
        String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
    }

    /**
     * 获取日期
     *
     * @param pattern
     * @return
     */
    public static final String getDateTime(String pattern) {
        return toString(new Date(), pattern);
    }

    /**
     * 比较日期大小（参数1的日期是否比参数2大）
     * @param bDate
     * @param eDate
     * @return 大于 ：1；小于：-1；等于：0
     */
    public static int compareTo(Date bDate, Date eDate) {
        if (bDate.getTime() > eDate.getTime()) {
            return 1;
        } else if (bDate.getTime() < eDate.getTime()) {
            return -1;
        } else {
            return 0;
        }
    }

    /**
     * 获取当天最小时间
     * @return
     */
    public static Date getThatDayMin(){
        Calendar todayStart = Calendar.getInstance();
        todayStart.set(Calendar.HOUR, 0);
        todayStart.set(Calendar.MINUTE, 0);
        todayStart.set(Calendar.SECOND, 0);
        todayStart.set(Calendar.MILLISECOND, 0);
        return todayStart.getTime();
    }

    /**
     * 获取当天最大时间 23.59.59
     * @return
     */
    public static Date getThatDayMax(){
        Calendar todayEnd = Calendar.getInstance();
        todayEnd.set(Calendar.HOUR, 23);
        todayEnd.set(Calendar.MINUTE, 59);
        todayEnd.set(Calendar.SECOND, 59);
        todayEnd.set(Calendar.MILLISECOND, 999);
        return todayEnd.getTime();
    }

    /**
     * 获取最小时间
     * @return
     */
    public static Date getDayMin(Date date){
        Calendar todayStart = Calendar.getInstance();
        todayStart.setTime(date);
        todayStart.set(Calendar.HOUR, 0);
        todayStart.set(Calendar.MINUTE, 0);
        todayStart.set(Calendar.SECOND, 0);
        todayStart.set(Calendar.MILLISECOND, 0);
        return todayStart.getTime();
    }

    /**
     * 获取最大时间 23.59.59
     * @return
     */
    public static Date getDayMax(Date date){
        Calendar todayEnd = Calendar.getInstance();
        todayEnd.setTime(date);
        todayEnd.set(Calendar.HOUR, 23);
        todayEnd.set(Calendar.MINUTE, 59);
        todayEnd.set(Calendar.SECOND, 59);
        todayEnd.set(Calendar.MILLISECOND, 999);
        return todayEnd.getTime();
    }

    /**
     * 获取最小时间
     * @return
     */
    public static Date getDayMin(int field, int amount){
        Calendar todayStart = Calendar.getInstance();
        todayStart.set(Calendar.HOUR, 0);
        todayStart.set(Calendar.MINUTE, 0);
        todayStart.set(Calendar.SECOND, 0);
        todayStart.set(Calendar.MILLISECOND, 0);
        todayStart.add(field, amount);
        return todayStart.getTime();
    }

    /**
     * 获取最小时间
     * @return
     */
    public static Date getDayMin(Date date, int field, int amount){
        Calendar todayStart = Calendar.getInstance();
        todayStart.setTime(date);
        todayStart.set(Calendar.HOUR, 0);
        todayStart.set(Calendar.MINUTE, 0);
        todayStart.set(Calendar.SECOND, 0);
        todayStart.set(Calendar.MILLISECOND, 0);
        todayStart.add(field, amount);
        return todayStart.getTime();
    }

    /**
     * 获取最大时间 23.59.59
     * @return
     */
    public static Date getDayMax(int field, int amount){
        Calendar todayEnd = Calendar.getInstance();
        todayEnd.set(Calendar.HOUR, 23);
        todayEnd.set(Calendar.MINUTE, 59);
        todayEnd.set(Calendar.SECOND, 59);
        todayEnd.set(Calendar.MILLISECOND, 999);
        todayEnd.add(field, amount);
        return todayEnd.getTime();
    }

    /**
     * 获取最大时间 23.59.59
     * @return
     */
    public static Date getDayMax(Date date, int field, int amount){
        Calendar todayEnd = Calendar.getInstance();
        todayEnd.setTime(date);
        todayEnd.set(Calendar.HOUR, 23);
        todayEnd.set(Calendar.MINUTE, 59);
        todayEnd.set(Calendar.SECOND, 59);
        todayEnd.set(Calendar.MILLISECOND, 999);
        todayEnd.add(field, amount);
        return todayEnd.getTime();
    }

    public static Date getMaxDayOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        calendar.set(Calendar.HOUR, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return calendar.getTime();
    }

    /**
     * 基于当前时间添加分钟
     * @param n 分钟数
     * @return
     */
    public static Date addMinute(int n) {
        return addMinute(new Date(), n);
    }

    /**
     * 基于指定时间添加分钟
     * @param date 指定时间
     * @param n 分钟数
     * @return
     */
    public static Date addMinute(Date date, int n) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MINUTE, n);
        return calendar.getTime();
    }

    /**
     * 基于指定时间添加秒
     * @param date 指定时间
     * @param n 秒数
     * @return
     */
    public static Date addSecond(Date date, int n) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.SECOND, n);
        return calendar.getTime();
    }

    public static String toString(Date date, String f) {
        if(date == null) return null;
        SimpleDateFormat sFormat = new SimpleDateFormat(f);
        return sFormat.format(date);
    }

    public static Date toDate(String str, String f) throws ParseException {
        if(StringUtil.isEmpty(str)) return null;
        SimpleDateFormat sFormat = new SimpleDateFormat(f);
        return sFormat.parse(str);
    }
}
